package com.demo.youxuanmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.demo.youxuanmall.common.Result;
import com.demo.youxuanmall.entity.SystemSetting;
import com.demo.youxuanmall.mapper.SystemSettingMapper;
import com.demo.youxuanmall.service.SystemSettingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class SystemSettingServiceImpl implements SystemSettingService {

    private final SystemSettingMapper systemSettingMapper;

    @Override
    public Result<Map<String, Map<String, Object>>> getAllSettings() {
        try {
            // 获取所有设置
            List<SystemSetting> allSettings = systemSettingMapper.selectList(null);
            
            // 按组分类
            Map<String, Map<String, Object>> groupedSettings = allSettings.stream()
                    .collect(Collectors.groupingBy(
                            SystemSetting::getSettingGroup,
                            Collectors.toMap(
                                    SystemSetting::getSettingKey,
                                    setting -> {
                                        Map<String, Object> settingMap = new HashMap<>();
                                        settingMap.put("value", setting.getSettingValue());
                                        settingMap.put("desc", setting.getSettingDesc());
                                        return settingMap;
                                    }
                            )
                    ));
            
            return Result.success(groupedSettings);
        } catch (Exception e) {
            log.error("获取所有设置失败", e);
            return Result.error("获取设置失败: " + e.getMessage());
        }
    }

    @Override
    public Result<Map<String, Object>> getSettingsByGroup(String group) {
        try {
            // 根据分组获取设置
            LambdaQueryWrapper<SystemSetting> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SystemSetting::getSettingGroup, group);
            
            List<SystemSetting> settings = systemSettingMapper.selectList(wrapper);
            
            Map<String, Object> result = settings.stream()
                    .collect(Collectors.toMap(
                            SystemSetting::getSettingKey,
                            setting -> {
                                Map<String, Object> settingMap = new HashMap<>();
                                settingMap.put("value", setting.getSettingValue());
                                settingMap.put("desc", setting.getSettingDesc());
                                return settingMap;
                            }
                    ));
            
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取分组设置失败", e);
            return Result.error("获取分组设置失败: " + e.getMessage());
        }
    }

    @Override
    public Result<String> getSettingValue(String key) {
        try {
            LambdaQueryWrapper<SystemSetting> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SystemSetting::getSettingKey, key);
            
            SystemSetting setting = systemSettingMapper.selectOne(wrapper);
            
            if (setting == null) {
                return Result.error("设置不存在");
            }
            
            return Result.success(setting.getSettingValue());
        } catch (Exception e) {
            log.error("获取设置值失败", e);
            return Result.error("获取设置值失败: " + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<Boolean> updateSettings(Map<String, String> settings) {
        try {
            if (settings == null || settings.isEmpty()) {
                return Result.error("设置不能为空");
            }
            
            for (Map.Entry<String, String> entry : settings.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                
                LambdaQueryWrapper<SystemSetting> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(SystemSetting::getSettingKey, key);
                
                SystemSetting setting = systemSettingMapper.selectOne(wrapper);
                
                if (setting != null) {
                    setting.setSettingValue(value);
                    systemSettingMapper.updateById(setting);
                }
            }
            
            return Result.success(true);
        } catch (Exception e) {
            log.error("更新设置失败", e);
            return Result.error("更新设置失败: " + e.getMessage());
        }
    }

    @Override
    public Result<Boolean> clearCache() {
        try {
            // 这里实现清除缓存的逻辑
            // 实际项目中可能需要清除Redis缓存或其他缓存系统
            log.info("系统缓存已清除");
            return Result.success(true);
        } catch (Exception e) {
            log.error("清除缓存失败", e);
            return Result.error("清除缓存失败: " + e.getMessage());
        }
    }
} 